//
//  LSWorkOvertimeDetailViewController.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2019/11/22.
//  Copyright © 2019 wpkj. All rights reserved.
//

#import "LSWorkOvertimeDetailViewController.h"
#import "LSLiuChengTuVC.h"

@interface LSWorkOvertimeDetailViewController ()

@end

@implementation LSWorkOvertimeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"加班详情";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"流程图" style:UIBarButtonItemStylePlain target:self action:@selector(checkStream)];
}

- (void)checkStream {
    LSLiuChengTuVC *lvc = [[LSLiuChengTuVC alloc] init];
    lvc.dict = self.responsObject;
    lvc.type = 2;
    [self.navigationController pushViewController:lvc animated:YES];
}

- (void)setTipTitleLabelArray:(NSArray *)tipTitleLabelArray {
    _tipTitleLabelArray = tipTitleLabelArray;
}

- (void)setInfoTitleLabelArray:(NSArray *)infoTitleLabelArray {
    _infoTitleLabelArray = infoTitleLabelArray;
}

- (void)setResponsObject:(NSDictionary *)responsObject {
    _responsObject = responsObject;
    [self setUpHeaderViewWithDict:_responsObject];
}

- (void)setUpHeaderViewWithDict:(NSDictionary *)responsObject {
    NSDictionary *dataDict = responsObject;
    
    UIView *headerInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44*self.tipTitleLabelArray.count+40)];
    headerInfoView.backgroundColor = UIColor.whiteColor;
    
    CGFloat currentHeight = 0.0f;
    for (int i = 0; i < self.tipTitleLabelArray.count; i++) {
        UILabel *tipTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 8+(44-8-15)/2+currentHeight, 100, 20)];
        tipTitleLabel.text = self.tipTitleLabelArray[i];
        tipTitleLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:14];
        tipTitleLabel.textColor = hexColor(333333);
        [tipTitleLabel sizeToFit];
        [headerInfoView addSubview:tipTitleLabel];
        
        UILabel *infoTitleLabel = [[UILabel alloc] init];
        infoTitleLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:14];
        infoTitleLabel.textColor = hexColor(333333);
        [headerInfoView addSubview:infoTitleLabel];
        infoTitleLabel.text = [ValidateUtils validateNull:dataDict[self.infoTitleLabelArray[i]]];
        if ([[ValidateUtils validateNull:self.tipTitleLabelArray[i]] rangeOfString:@"时长"].length > 0) {
            infoTitleLabel.text = [NSString stringWithFormat:@"%.1f小时",[ValidateUtils validateNull:dataDict[self.infoTitleLabelArray[i]]].floatValue];
        }
        if ([[ValidateUtils validateNull:self.tipTitleLabelArray[i]] rangeOfString:@"时间"].length > 0) {
            infoTitleLabel.text = [self timeStampToDate:[NSString stringWithFormat:@"%.1f小时",[ValidateUtils validateNull:dataDict[self.infoTitleLabelArray[i]]].floatValue]];
        }
        if ([[ValidateUtils validateNull:self.tipTitleLabelArray[i]] rangeOfString:@"职务"].length > 0) {
            [self requestListForTextField:infoTitleLabel withURLString:OASjzd andParam:@{@"typeGroupName":@"userDuty",@"token":[ValidateUtils validateNull:[UserInfoModel sharedInstance].token]} listCode:@"obj" showCode:@"typename" requestCode:@"typecode" matchCode:[ValidateUtils validateNull:dataDict[self.infoTitleLabelArray[i]]]];
        }
        if ([[ValidateUtils validateNull:self.tipTitleLabelArray[i]] rangeOfString:@"是否"].length > 0) {
            if ([ValidateUtils validateNull:dataDict[self.infoTitleLabelArray[i]]].intValue > 0) {
                infoTitleLabel.text = @"是";
            } else {
                infoTitleLabel.text = @"否";
            }
        }
        if ([[ValidateUtils validateNull:self.tipTitleLabelArray[i]] rangeOfString:@"事由"].length > 0) {
            CGRect TitleRect = [[ValidateUtils validateNull:dataDict[self.infoTitleLabelArray[i]]] boundingRectWithSize:CGSizeMake(ScreenWidth-45, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont fontWithName:@"SourceHanSansCN-Regular" size:14]} context:nil];
            [infoTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(headerInfoView.mas_left).with.offset(30);
                make.top.equalTo(tipTitleLabel.mas_bottom).with.offset(8);
                make.right.equalTo(headerInfoView.mas_right).with.offset(-15);
                make.height.mas_equalTo(TitleRect.size.height+8+12);
            }];
            infoTitleLabel.numberOfLines = 0;
            currentHeight += TitleRect.size.height+8+12+8+8+(44-8-15);

            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, currentHeight, ScreenWidth-30, 0.5)];
            lineView.backgroundColor = hexColor(dbdbdb);
            [headerInfoView addSubview:lineView];
        } else {
            [infoTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(tipTitleLabel.mas_right).with.offset(2);
                make.centerY.equalTo(tipTitleLabel.mas_centerY);
                make.right.equalTo(headerInfoView.mas_right).with.offset(-12);
                make.height.equalTo(@20);
            }];
            currentHeight += 44;
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, currentHeight, ScreenWidth-30, 0.5)];
            lineView.backgroundColor = hexColor(dbdbdb);
            [headerInfoView addSubview:lineView];
        }
    }
    headerInfoView.frame = CGRectMake(0, 0, ScreenWidth, currentHeight+40);
    [self.view addSubview:headerInfoView];
}

- (void)requestListForTextField:(UILabel *)label withURLString:(NSString *)URLString andParam:(NSDictionary *)param listCode:(NSString *)listCode showCode:(NSString *)showCode requestCode:(NSString *)requestCode matchCode:(NSString *)matchCode {
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:URLString param:param completionHandler:^(id responseObject, NSError *connectionError) {
        if (!connectionError) {
            NSArray *objArray = [responseObject objectForKey:listCode];
            if (objArray && objArray.count > 0) {
                for (NSDictionary *dict in objArray) {
                    if ([[ValidateUtils validateNull:dict[@"typecode"]] isEqualToString:matchCode]) {
                        label.text = [ValidateUtils validateNull:dict[@"typename"]];
                        break;
                    }
                }
            }
        } else {
            [MBProgressHUD showTipMessageInWindow:[NSString stringWithFormat:@"%@",connectionError.localizedDescription]];
        }
    }];
}

- (NSString *)timeStampToDate:(NSString *)timeStampString {
    // iOS 生成的时间戳是10位
    if (![[ValidateUtils validateNull:timeStampString] isEqualToString:@""] && !([ValidateUtils validateNull:timeStampString].integerValue == 0)) {
        NSTimeInterval interval = [timeStampString doubleValue] / 1000.0;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        return [formatter stringFromDate:date];
    } else {
        return @"";
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
